﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Controls;

namespace ChoDiaOc_HK_WPF.Views.QuanLy {
    /// <summary>
    /// Interaction logic for QuanLyLoaiBao.xaml
    /// </summary>
    public partial class QuanLyLoaiBao : UserControl {
        public QuanLyLoaiBao() {
            InitializeComponent();
            entities = new DBEntities();

        }
        private DBEntities entities;

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["bAOsViewSource"];
            myCollectionViewSource.Source = entities.BAOs;
        
        }

        private void btnThem_Click(object sender, RoutedEventArgs e) {
            if (bAOsListView.SelectedIndex != -1) {
                bAOsListView.SelectedIndex = -1;
            } else {
                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {

                    BAO dn = new BAO();
                    dn.MaBao = TiepNhanDangKyController.GetInstance().GenerateMaBAO();
                    dn.TenBao = tenBaoTextBox.Text;
                    dn.GiaTien = decimal.Parse(giaTienTextBox.Text);

                    entities.AddToBAOs(dn);
                    entities.SaveChanges();
                    System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["bAOsViewSource"];
                    entities = new DBEntities();
                    myCollectionViewSource.Source = entities.BAOs;
                }
            }

        }

        private void btnXoa_Click(object sender, RoutedEventArgs e) {
            if (bAOsListView.SelectedIndex != -1) {
                BAO dn = bAOsListView.SelectedItem as BAO;

                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {
                    entities.BAOs.DeleteObject(bAOsListView.SelectedItem as BAO);
                    entities.SaveChanges();
                }
            }
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e) {
            try {
                entities.SaveChanges();
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            } catch (Exception exp) {
                MessageBox.Show(exp.Message);
            }

        }

        private void btnNhapLai_Click(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["bAOsViewSource"];
            entities = new DBEntities();
            myCollectionViewSource.Source = entities.BAOs;

        }
    }
}
